<?php

namespace app\home\controller;

use think\Controller;
use think\Request;
use think\Session;

class Detail extends Controller
{

    public function det($id)
    {
        if (Session::has('email')){
            $email = Session::get('email');
        }else{
            $email = '11';
        }
        //查询所有作品 按分数查8个
        $datall = db('zuopin')->limit(8)->order('mark desc')->select();
        //查询所有作品 按时间查8个
        $timeall = db('zuopin')->limit(8)->order('createtime desc')->select();
        //查找该作品信息
        $data = db('zuopin')->where('id','eq',$id)->find();
        //查找该分类
        $fname = db('fenlei')->where('id','eq',$data['fid'])->field('name')->find();
        //查找所有目录 倒叙
        $mulu = db('mulu')->where('zid','eq',$data['id'])->order('id desc')->select();
        //最新目录
        $xin = db('mulu')->limit(1)->where('zid','eq',$data['id'])->order('id desc')->find();
//        dump($xin);die;
        //查找该分类下的5个作品
        $zuo = db('zuopin')->where('fid','eq',$data['fid'])->limit(5)->order('id desc')->select();
        //查找目录总数
        $cid = db('mulu')->where('zid','eq',$data['id'])->count('id');

        //查看评论
        $comment = db('comment')->where('mid','eq',$id)->paginate(10);
        $zong = db('comment')->where('mid','eq',$id)->count('id');
        return view('detail',[
            'datall'=>$datall,
            'timeall' => $timeall,
            'data'=>$data,
            'fname'=>$fname['name'],
            'mulu' => $mulu,
            'cid' => $cid,
            'xin' => $xin,
            'zuo' => $zuo,
            'comment' => $comment,
            'email' => $email,
            'did' => '4', //登陆用户
            'zong' => $zong
        ]);
    }

    public function read($id){
        if (Session::has('email')){
            $email = Session::get('email');
        }else{
            $email = '11';
        }
        //查询所有作品 按分数查8个
        $datall = db('zuopin')->limit(8)->order('mark desc')->select();
        //查询所有作品 按时间查8个
        $timeall = db('zuopin')->limit(8)->order('createtime desc')->select();
        //查找该作品信息
        $data = db('zuopin')->where('id','eq',$id)->find();
        //查找该分类
        $fname = db('fenlei')->where('id','eq',$data['fid'])->field('name')->find();
        //查找所有目录 倒叙
        $mulu = db('mulu')->where('zid','eq',$data['id'])->order('id desc')->select();
        //最新目录
        $xin = db('mulu')->limit(1)->where('zid','eq',$data['id'])->order('id desc')->find();
//        dump($xin);die;
        //查找该分类下的5个作品
        $zuo = db('zuopin')->where('fid','eq',$data['fid'])->limit(5)->order('id desc')->select();
        //查找目录总数
        $cid = db('mulu')->where('zid','eq',$data['id'])->count('id');

        //查看评论
        $comment = db('comment')->where('mid','eq',$id)->paginate(10);
        $zong = db('comment')->where('mid','eq',$id)->count('id');
        return view('detail',[
            'datall'=>$datall,
            'timeall' => $timeall,
            'data'=>$data,
            'fname'=>$fname['name'],
            'mulu' => $mulu,
            'cid' => $cid,
            'xin' => $xin,
            'zuo' => $zuo,
            'comment' => $comment,
            'email' => $email,
            'did' => '4',
            'zong'=>$zong
        ]);
    }

    //赞
    public function update(Request $request, $id)
    {
//        dump($dataid);die;
        $p = $request->put();
        $panzan = $p['len'];
        if ($panzan == 1){
            $ref = db('comment')->where('id','eq',$id)->field('zan')->find();
            $tr = $ref['zan'];
            $tr++;
            $data = [
                'zan'=>$tr
            ];
            $res = db('comment')->where('id','eq',$id)->update($data);
            if ($res){
                $info['status'] = true;
                $info['infoo'] = '已赞';
                $info['zanzz'] = $tr;
                $info['id'] = $id;
            }else{
                $info['status'] = false;
            }

            return json($info);
        }else{
            $ref = db('comment')->where('id','eq',$id)->field('zan')->find();
            $tr = $ref['zan'];
            $tr--;
            $data = [
                'zan'=>$tr
            ];
            $res = db('comment')->where('id','eq',$id)->update($data);
            if ($res){
                $info['status'] = true;
                $info['infoo'] = '赞';
                $info['zanzz'] = $tr;
                $info['id'] = $id;
            }else{
                $info['status'] = false;
            }

            return json($info);
        }

    }


    //评论
    public function save(Request $request){

        $tt = $request->post();
//        dump($tt);die;
        $time = time();
        $tie = date('Y-m-d H:i:s',time());
        $name = Session::get('name');
        $name = 'hddd';
        $data = [
            'detail'=>$tt['detail'],
            'mid' =>$tt['mid'],
            'time' =>$time,
            'name' =>$name
        ];

        if (empty($data['detail'])){
            $info['status'] = false;
            return json($info);
        }else{
            $res = db('comment')->insertGetId($data);
//            dump($res);die;
            $zan = db('comment')->where('id','eq',$res)->field('zan')->find();
            if ($res > 0){
                $info['status'] = true;
                $info['detail'] = $data['detail'];
                $info['mid'] = $data['mid'];
                $info['tie'] = $tie;
                $info['name'] = $data['name'];
                $info['zang'] = $zan['zan'];
                $info['id'] = $res;

            }else{
                $info['status'] = false;
            }
            return json($info);
        }

    }

}
